﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyWindowsService
{
    public partial class dlgConfig : Form
    {
        public dlgConfig()
        {
            InitializeComponent();
            this.DialogResult = DialogResult.Cancel;
        }

        private void dlgConfig_Load(object sender, EventArgs e)
        {
            MyConfig.Instance.Load();
            if (MyConfig.Instance.WatchedPaths != null)
            {
                lstPaths.Items.AddRange(MyConfig.Instance.WatchedPaths);
            }
            chkLogChanged.Checked = MyConfig.Instance.LogChanged;
            chkLogCreated.Checked = MyConfig.Instance.LogCreated;
            chkLogDeleted.Checked = MyConfig.Instance.LogDeleted;
            chkLogRenamed.Checked = MyConfig.Instance.LogRenamed;
        }

        private void cmdDeletePath_Click(object sender, EventArgs e)
        {
            if (lstPaths.SelectedIndex >= 0)
            {
                lstPaths.Items.RemoveAt(lstPaths.SelectedIndex);
            }
        }

        private void cmdBrowsePath_Click(object sender, EventArgs e)
        {
            using (System.Windows.Forms.FolderBrowserDialog dlg = new FolderBrowserDialog())
            {
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    txtPath.Text = dlg.SelectedPath;
                }
            }
        }

        private void cmdAddPath_Click(object sender, EventArgs e)
        {
            string txt = txtPath.Text.Trim();
            if (txt.Length > 0)
            {
                lstPaths.Items.Add(txt);
            }
        }

        private void cmdOk_Click(object sender, EventArgs e)
        {
            MyConfig.Instance.LogChanged = chkLogChanged.Checked;
            MyConfig.Instance.LogCreated = chkLogCreated.Checked;
            MyConfig.Instance.LogDeleted = chkLogDeleted.Checked;
            MyConfig.Instance.LogRenamed = chkLogRenamed.Checked;

            System.Collections.ArrayList paths = new System.Collections.ArrayList();
            foreach (object item in lstPaths.Items)
            {
                paths.Add(Convert.ToString(item));
            }
            MyConfig.Instance.WatchedPaths = (string[])paths.ToArray(typeof(string));
            MyConfig.Instance.Save();
            this.DialogResult = DialogResult.OK;
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}